# Maintainer: Rémi BERTHO <remi.bertho@dalan.fr>

_realname=podofo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.8
pkgrel=5
pkgdesc="A C++ library to work with the PDF file format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://podofo.sourceforge.io/"
license=('spdx:LGPL-2.0-or-later AND GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libunistring"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cppunit")
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        "find-modules.patch"
        "fix-export.patch"
        "003-podofo-0.9.7-localtime_r.patch"
        "https://github.com/podofo/podofo/commit/d0e9f5d503b0cb79516ec9bff989f3d7d625b678.patch")
sha256sums=('5de607e15f192b8ad90738300759d88dea0f5ccdce3bf00048a0c932bc645154'
            '9e04e9a009520f12876d9c67b2af44bab5266782a59d53057a59d26f0e8a4294'
            'cd81e8989ad809f249099d4d14c667b5d65637f9d1bfb711aa49a0bd8afe269b'
            'ee96bf3a1d7acc299d28435b3b0ad4a10a77b32c4a261b54765afd9d14bea087'
            '6eaab2d5de5de8806f2b270c086279c7bc812040f245a924765f77d048d835d8')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/find-modules.patch"
  patch -p1 -i "${srcdir}/fix-export.patch"
  patch -p1 -i "${srcdir}/003-podofo-0.9.7-localtime_r.patch"

  # GCC 12 build fix backport: https://github.com/podofo/podofo/pull/5
  patch -p1 -i "${srcdir}/d0e9f5d503b0cb79516ec9bff989f3d7d625b678.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -Wno-dev \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DFREETYPE_INCLUDE_DIR=${MINGW_PREFIX}/include/freetype2 \
    ${_extra_config[@]} \
    -DPODOFO_BUILD_SHARED=ON \
    -DPODOFO_HAVE_JPEG_LIB=ON \
    -DPODOFO_HAVE_PNG_LIB=ON \
    -DPODOFO_HAVE_TIFF_LIB=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd build-${MSYSTEM}
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .
}
